<?php 
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use cmf\controller\BaseController;
use think\Db;

	class WxloginController extends BaseController
	{
		
		function login(){
			// $obj=new RedisController;
			// $obj -> delete();
		// $data=array('appid'=>'wx0f912ae0a6bd22ea&secret=428d9485b1b696adc863e647ddcb29ce','js_code'=>'001mwwL92ZmAcO02PPL92NvuL92mwwLI','grant_type'=>'authorization_code');
		$url='http://v2.crejoy.cn/user/login/user_info';
		$token = $this->request->param('token');
		// db()
		$data['token']=$token;
		$info = sqcurl($url, 'post', $data, array());
		$info=json_decode($info,true);
		// echo count($info['data']['data']);
		// var_dump($info);
		// exit;
	
		$staff_info=db('staff_member')->where('app_userid',$info['data']['id'])->field('id,business_card,staff_name,staff_phone,app_company_name,app_position')->find();
	
		//exit;
		if($staff_info){
		
			// return info_back(array('staff_id'=>$staff_id),'请求成功',true);
			return json_encode($staff_info);


		}else{




			// return info_back(array(),'请求失败',false);
			$add_info['app_userid']=$info['data']['id'];
			$add_info['app_user_type']=$info['data']['user_type'];
			$add_info['app_user_email']=$info['data']['user_email'];
			$add_info['app_sex']=$info['data']['sex'];
			$add_info['app_user_nickname']=$info['data']['user_nickname'];
			$add_info['app_staff_mobile']=$info['data']['mobile'];
			$add_info['app_create_time']=$info['data']['create_time'];
			$add_info['app_update_time']=$info['data']['update_time'];
			$add_info['app_area_id']=$info['data']['area_id'];
			$add_info['app_industry']=$info['data']['industry_id'];
			$add_info['app_position']=$info['data']['position'];
			$add_info['app_company_name']=$info['data']['company_name'];
			$add_info['app_company_tel']=$info['data']['company_tel'];
			$add_info['app_web_url']=$info['data']['web_url'];
			$add_info['app_logo']=$info['data']['logo'];
			$add_info['app_address']=$info['data']['address'];
			$add_info['app_business']=$info['data']['business'];
			$add_info['app_check_status']=$info['data']['check_status'];
			$add_info['app_user_login']=$info['data']['user_login'];
			$add_info['app_user_pass']=$info['data']['user_pass'];
			$add_info['app_user_status']=$info['data']['user_status'];
			$add_info['app_im_token']=$info['data']['im_token'];
			$add_info['app_accid']=$info['data']['accid'];
			$add_info['app_coin']=$info['data']['coin'];
			$add_info['app_avatar']=$info['data']['avatar'];
			$add_info['app_test_sign']=$info['data']['test_sign'];
			$add_info['app_receive_coin']=$info['data']['receive_coin'];
			$add_info['app_ali_id']=$info['data']['ali_id'];
			$add_info['app_ali_name']=$info['data']['ali_name'];
			$add_info['app_token']=$info['data']['token'];
			$add_info['app_limit_amount']=$info['data']['limit_amount'];
			$staff_id=db('staff_member')->insert($add_info);
			if($staff_id){
				$staff_info=db('staff_member')->where('app_userid',$info['data']['id'])->field('id,business_card,staff_name,staff_phone,app_company_name,app_position,app_avatar,customer_id')->find();
				// return db('staff_member')->getLastInsID();
				return json_encode($staff_info);


			}else{
				return '用户信息更新失败';
			}

		}
		
		// var_dump($add_info);

	}














































	function add_staff_info(){
		$data['id'] = $this->request->param('staff_id');
		$business_card = $this->request->param('business_card','');
		if($business_card){
			$data['business_card']=$business_card;
		}
		//$apply_data['is_audit'] = $this->request->param('is_audit',1);
		$staff_name=$this->request->param('staff_name');
		if($staff_name){
			$data['staff_name']=$staff_name;
		}
		$staff_phone=$this->request->param('staff_phone');
		if($staff_phone){
			$data['staff_phone']=$staff_phone;
		}
		//$app_company_name=$this->request->param('company_name');
		// if($app_company_name){
		// 	$data['app_company_name']=$app_company_name;
		// }
		$app_position=$this->request->param('position');
		if($app_position){
			$data['app_position']=$app_position;
		}
		$customer_id=$this->request->param('customer_id');
		if($customer_id){
			$data['customer_id']=$customer_id;
		}
		$apply_data['site_id']=$this->request->param('site_id');
		$apply_data['staff_id']=$this->request->param('staff_id');
		$apply_data['start_time']=$this->request->param('start_time');
		$apply_data['end_time']=$this->request->param('end_time');
		$apply_data['add_time']=time();
		$remark=$this->request->param('remark','');
		if($remark){
			$apply_data['remark']=$remark;
		}
		$apply_data['is_audit'] = $this->request->param('is_audit',1);
		if($apply_data['is_audit'] == 2){
			$apply_data['audit_status'] = 1;
		}else{
			$apply_data['audit_status'] = 2;
		}
		// if($apply_data)
		// $where['start_time < ']= $apply_data['start_time'];
		// $where['end_time > ']= $apply_data['start_time'];
		// $where['start_time < ']= $apply_data['end_time'];
		// $where['end_time > ']= $apply_data['end_time'];
		// $where1['start_time > ']= $apply_data['start_time'];
		// $where1['end_time > ']= $apply_data['end_time'];
		
		if($apply_data['start_time'] > $apply_data['end_time']){
			$a = $apply_data['start_time'];
			$apply_data['start_time'] = $apply_data['end_time'];
			$apply_data['end_time'] = $a ;
		}
		
		$where='(start_time >  "'.$apply_data['start_time'].'" and start_time <  "'.$apply_data['end_time'].'" and site_id = "'.$apply_data['site_id'].'" and is_using != 6) or (end_time >  "'.$apply_data['start_time'].'"  and end_time <  "'.$apply_data['end_time'].'" and site_id = "'.$apply_data['site_id'].'" and is_using != 6 ) or  (start_time <=  "'.$apply_data['start_time'].'"  and end_time >=  "'.$apply_data['end_time'].'" and site_id = "'.$apply_data['site_id'].'" and is_using != 6 ) or  (start_time >=  "'.$apply_data['start_time'].'"  and end_time <=  "'.$apply_data['end_time'].'" and site_id = "'.$apply_data['site_id'].'" and is_using != 6 ) ';
		$distinc=db('site_apply_info')->where($where)->select();
		//var_dump($distinc);
		// var_dump($distinc);
		// /echo count($distinc);
		// if(count($distinc)){
		// 	return "该时间段已被预订预定";
		// }
		
		if(empty($data['id']) && empty($data['business_card']) &&  empty($data['staff_name']) && empty($data['staff_phone'])  && empty($data['app_position'])){
			return '确认预约信息不完整';
		}
		
		$sure = db('staff_member')->update($data);
		$apply=db('site_apply_info')->insert($apply_data);
		if($apply){
			return 1;
		}else{
			return 0;
		}

	}
	function editStaff(){
		$data['app_avatar'] = $this->request->param('app_avatar');
		$data['id'] = $this->request->param('staff_id',0);
		$data['staff_name']=$this->request->param('staff_name');
		$data['staff_phone']=$this->request->param('staff_phone');
		// $data['app_company_name']=$this->request->param('company_name');
		$data['app_position']=$this->request->param('app_position');
		$data['customer_id']=$this->request->param('customer_id');
		$update=db('staff_member')->update($data);

		if($update){
			// return 1;
			return $this->get_userinfos($data['id']);
		}else{
			return 0;
		}
	
	}
	function tip(){
		$where=array();
		$arr=array();
		$str = $this->request->param('customer_name',''); 
	
		$where['status']=1;
		$str_array=db('customer')->where('customer_name','like','%'.$str.'%')->where($where)->limit(10)->select();
		
		foreach ($str_array as $key => $value) {
			$arr[$key]['customer_id']=$value['id'];
			$arr[$key]['customer_name']=$value['customer_name'];
		}
		return json($arr);
		// $str1=implode(',', $arr);
		// echo $str1;  
	}




	function get_userinfos($staff_id){
		// $data['id'] = $this->request->param('staff_id');
		$data['id'] = $staff_id;
		$staff_info=db('staff_member')->where('id',$data['id'])->field('id,business_card,staff_name,staff_name,staff_phone,app_company_name,app_position,app_avatar,app_logo,customer_id')->find();
		if(!$staff_info){
			return '该员工不存在';
		}
		$staff_info['customer_name']=get_customer_name($staff_info['customer_id']);
		$where['staff_id'] = $data['id'];
		$where['audit_status'] = 2;
		$where1['staff_id'] = $data['id'];
		$where1['audit_status'] = 6;
		//$where2=' staff_id = "'.$data['staff_id'].'" and start_time > "'.time().'" ' ;
		$where2=' staff_id = "'.$data['id'].'" and start_time > "'.time().'" ' ;
		$condition1='(is_audit = 1 ) or (is_audit =2 and audit_status =2)';
		$condition=' start_time <= "'.time().'" and end_time >= "'.time().'" and staff_id = "'.$data['id'].'" ';
		$arr['cancel_num']['num']= db('site_apply_info')->where($where1)->count();
		$arr['cancel_num']['name']='取消';
		$arr['cancel_num']['type']=3;
		$arr['using_num']['num']= db('site_apply_info')->where($condition)->where($condition1)->count();
		$arr['using_num']['name']='正在使用';
		$arr['using_num']['type']=2;
		$arr['allow_num']['num']=db('site_apply_info')->where($where2)->where($condition1)->count();
		$arr['allow_num']['name']='预订成功';
		$arr['allow_num']['type']=1;
		$staff_info=array_merge($staff_info,$arr);
		return json($staff_info);
	}

	function get_userinfo(){
		$data['id'] = $this->request->param('staff_id');
		$site_id = $this->request->param('site_id','');
		$staff_info=db('staff_member')->where('id',$data['id'])->field('id,business_card,staff_name,staff_name,staff_phone,app_company_name,app_position,app_avatar,app_logo,customer_id')->find();
		if(!$staff_info){
			return '该员工不存在';
		}
		if($site_id){
			// echo 2222;
			// exit;
			$site_address = db('site')->where('id',$site_id)->value('address');
			// var_dump($site_address);
			// exit;
			$staff_info['site_address'] = $site_address ;
			// var_dump($staff_info);
			// exit;
		}else{
			$staff_info['site_address'] = '暂无' ;
		}
		$staff_info['customer_name']=get_customer_name($staff_info['customer_id']);
		$where['staff_id'] = $data['id'];
		$where['audit_status'] = 2;
		$where1['staff_id'] = $data['id'];
		$where1['is_using'] = 6;
		//$where2=' staff_id = "'.$data['staff_id'].'" and start_time > "'.time().'" ' ;
		$where2=' staff_id = "'.$data['id'].'" and start_time > "'.time().'" ' ;
		// $condition1='(is_audit = 1 ) or (is_audit =2 and audit_status =2)';
		$condition1=' audit_status =2 ';
		$condition=' start_time <= "'.time().'" and end_time >= "'.time().'" and staff_id = "'.$data['id'].'" ';
		$arr['cancel_num']['num']= db('site_apply_info')->where($where1)->count();
		$arr['cancel_num']['name']='取消';
		$arr['cancel_num']['type']=3;
		$arr['using_num']['num']= db('site_apply_info')->where($condition)->where($condition1)->count();
		$arr['using_num']['name']='正在使用';
		$arr['using_num']['type']=2;
		$arr['allow_num']['num']=db('site_apply_info')->where($where2)->where($condition1)->count();
		$arr['allow_num']['name']='预订成功';
		$arr['allow_num']['type']=1;
		$staff_info=array_merge($staff_info,$arr);
		return json($staff_info);
	}















function test(){
	if($_POST){
		$b='';
		$a=[1,23,4,56,7,8];
		$b.='<form action="" method="post">
		服务器地址';
		foreach ($a as $key => $value) {
			$b.='<input type="text" name="host" value="'.$value.'" />';
		}


		
		$b.='<input type="text" name="host" value="localhost" /><br />
		数据库账号：<input type="text" name="user" value="" /><br />
		数据库密码：<input type="password" name="pwd" value="" /><br />
		指定数据库：<input type="text" name="db" value="test" /><br />
		<input type="submit" value="确定"/>
	    </form>';
	    echo $b;
    }
}




















   }